//created by: Justina Lee

Ext.define('MajorProjectSenchaTouch.controller.OpeningHours', {
    extend: 'Ext.app.Controller',
    config:{
        views: ['OpeningHoursModule.OpeningHoursTermDataview'],
        refs:{
            termButton: 'button[action=goTerm]',
            vacationBreakButton: 'button[action=goVacationBreak]',
            mainPanel: 'openinghoursMainBody',
            openingHoursTermDV: 'openinghoursTermDV',
            openingHoursVBDV:'openinghoursVBDV'
        },
        control:{
            termButton:{
                tap: 'goTerm'
            },
            vacationBreakButton:{
                tap: 'goVacationBreak'
            },
            openingHoursTermDV:{
                showIndicator: 'initScrollbar'
            },
            openingHoursVBDV:{
                showIndicator: 'initScrollbar'
            }
        }
    },
    /*
|--------------------------------------------------------------------------
| goVacationBreak (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function is triggered when the user tap on the "Vacation/Break" button.
| The openinghoursVB dataview is refreshed and the panel is set as the active item.
|
*/
    goVacationBreak: function(){    
        var vbDV = this.getOpeningHoursVBDV();
        vbDV.refresh();
        
        var mainPanel = this.getMainPanel();
        mainPanel.animateActiveItem(1, this.slideLeftTransition);
    },
    
    /*
|--------------------------------------------------------------------------
| goTerm (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function is triggered when the user tap on the "Term" button.
| The openinghoursTerm dataview is refreshed and the panel is set as the active item.
|
*/
    goTerm: function(){   
        var termDV = this.getOpeningHoursTermDV();
        termDV.refresh();
        
        var mainPanel = this.getMainPanel();
        mainPanel.animateActiveItem(0, this.slideRightTransition);
    },
    
    /*
|--------------------------------------------------------------------------
| getCurrentOpeningHours (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Checks which term/vacation does the current date falls within. Navigates to
| the tab where the current fate falls within.
|
*/
    getCurrentOpeningHours: function(){
        var ctrl = this;
        var mainCtrl = this.getApplication().getController('Main');
        
        var view = mainCtrl.getNavView();
        var previousPage = view._activeItem;
        
        var termStore = Ext.getStore('OpeningHoursTermInfo');
        var vbStore = Ext.getStore('OpeningHoursVBInfo');
        
        //get first record from each store and check if within range
        termStore.load({
            callback: function(){          
                vbStore.load({
                    callback: function(){
                        var vbRecord = vbStore._data.items[0];
                        var vbDateRangeString = vbRecord.data.termDate;
               
                        var isWithinVB = ctrl.checkDateWithinRange(vbDateRangeString);
                        mainCtrl.pushView('Opening Hours', 'openinghourspage');
                        
                        if(isWithinVB){
                            ctrl.goVacationBreak();                           
                            
                            //index of vacation tab is 1.
                            var ohSegBtn = mainCtrl.getOhSegmentedBtn();
                            ohSegBtn.setPressedButtons([1]);
                        }
                        
                        mainCtrl.resetHomeSegBtn();
                        mainCtrl.hideMask(previousPage);
                    } 
                });
            } 
        });
    },
    
    /*
|--------------------------------------------------------------------------
| checkDateWithinRange (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Parses the date range string to get the start date and end date. Returns true,
| if current date is within the start date and end date range. Else, return false.
|
*/
    checkDateWithinRange: function(dateRangeString){
        var startDateString = dateRangeString.substr(0, dateRangeString.lastIndexOf(" - "));
        var endDateString = dateRangeString.substr(dateRangeString.lastIndexOf("-") + 2);
        
        var endDate = new Date(endDateString);
        var startDate = new Date(startDateString);
        var currentDate = new Date();
        
        if(currentDate >= startDate && currentDate <= endDate){
            return true;
        }else{
            return false;
        }
        
    },
    /*
|--------------------------------------------------------------------------
| initScrollbar (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Override the onScrollEnd event such that the scroller will not hide when reached
| the end of the list. 
| Set the scroller to show.
|
*/
    initScrollbar: function(dataView){
        //override onScrollEnd event
        var ctrl = MajorProjectSenchaTouch.app.getController('Override');
        ctrl.onScrollEndBlank();
        
        //show indicators
        var scroller = dataView.getScrollable();
        scroller.showIndicators()
    },
    /*
|--------------------------------------------------------------------------
| slideLeftTransition, slideRightTransition (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Transition configurations.
|
*/
    slideLeftTransition: {
        type: 'slide', 
        direction: 'left'
    },
    slideRightTransition: {
        type: 'slide', 
        direction: 'right'
    }
});

